package com.thuanhoang.boo.adapter;

import java.util.ArrayList;

import thirdparty.SquareImageView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.thuanhoang.boo.R;
import com.thuanhoang.boo.model.Product;

public class ProductListAdapter extends BaseAdapter {
	private ArrayList<Product> productList;
	private LayoutInflater layoutInflater;
	private Context context;

	public ProductListAdapter(Context context, ArrayList<Product> productList) {
		this.productList = productList;
		this.layoutInflater = LayoutInflater.from(context);
		this.context = context;
		layoutInflater = LayoutInflater.from(context);
	}

	public Context getContext() {
		return context;
	}

	public int getCount() {
		return productList.size();
	}

	public Object getItem(int position) {
		return productList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ProductViewHolder holder;
		Product product_item;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.product_item, parent,false);
			holder = new ProductViewHolder();
			holder.productImage = (SquareImageView) convertView.findViewById(R.id.productImage);
			holder.productName =(TextView) convertView.findViewById(R.id.productName);
			holder.productPrice =(TextView) convertView.findViewById(R.id.productPrice);

			convertView.setTag(holder);
		} else {
			holder = (ProductViewHolder) convertView.getTag();
		}
		try {
			product_item = productList.get(position);
		} catch (Exception e) {
			return null;
		}

		holder.productName.setText(product_item.getProductName());
		holder.productPrice.setText(product_item.getPrice());
		if(product_item.getProductImageLink()!=null && product_item.getProductImageLink() !=""){
			ImageLoader.getInstance().displayImage(product_item.getProductImageLink(),
					holder.productImage);
		}
		
		return convertView;
	}

	public static class ProductViewHolder {
		SquareImageView productImage;
		TextView productName;
		TextView productPrice;

	}
}
